<?
	class Repository
	{
		public $meta;
		
		protected $entity;
		
		function __construct($entity = null)
		{
			if(isset($this->entity)){
				$entity = $this->entity;
			}
			
			$this->meta = new EntityMeta($entity);
		}
		
		public function newQuery()
		{
			return new Query($this->meta->table);
		}

        public function query($id = null, $props = true)
        {
            $query = $this->newQuery();

			if($props)
			{
				foreach($this->meta->props as $prop)
				{
					if($prop['name'] == $prop['sqlName'])
					{
						$query->field($prop['name']);
					}
					else
					{
						$query->field($prop['sqlName'], $prop['name']);
					}
				}
			}

            if(isset($id))
            {
                $query->where(array(
                    'eq' =>  array($this->meta->idProperty, $id)
                ));
            }

            return $query;
        }
		
		public function get($id)
		{
            return $this->query($id)->result();
		}

        public function remove($id)
        {
            return $this->query($id)->remove();
        }

        public function insert($data)
        {
            $id = $this->query()->insert($data);

            return $this->get($id);
        }
		
		public function update($data, $id = null)
		{
			$query = $this->query($id);
			$query->update($data);
		}
	}
?>